package com.coderush.admin.sys.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.coderush.admin.core.common.MyBatisPlusPageUtils;
import com.coderush.admin.core.common.PageResult;
import com.coderush.admin.core.component.SnowflakeIdGenerator;
import com.coderush.admin.sys.dao.JobDao;
import com.coderush.admin.sys.exception.JobException;
import com.coderush.admin.sys.model.dto.JobAddDto;
import com.coderush.admin.sys.model.dto.JobUpdateDto;
import com.coderush.admin.sys.model.entity.Company;
import com.coderush.admin.sys.model.entity.Job;
import com.coderush.admin.sys.model.vo.JobPageVo;
import com.coderush.admin.sys.service.JobService;
import com.coderush.admin.sys.mapper.JobMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

/**
* @author Administrator
* @description 针对表【sys_job(职位表)】的数据库操作Service实现
* @createDate 2025-08-25 14:21:20
*/
@Service
public class JobServiceImpl extends ServiceImpl<JobMapper, Job> implements JobService{

    @Resource
    private JobDao jobDao;
    @Resource
    private SnowflakeIdGenerator snowflakeIdGenerator;

    /**
     * 分页查询职位列表
     *
     * @param jobName
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageResult<JobPageVo> findByPage(String jobName, Integer pageNum, Integer pageSize) {

        // 构建分页对象
        Page<JobPageVo> page = MyBatisPlusPageUtils.buildPage(pageNum, pageSize);
        // 执行分页查询（会自动分页）
        Page<JobPageVo> usePage = jobDao.findByPage(page, jobName);

        // 将 MyBatis-Plus 的 IPage 转换为统一的 PageResult
        PageResult<JobPageVo> result = MyBatisPlusPageUtils.toPageResult(usePage);
        result.setList(usePage.getRecords());

        return result;
    }

    /**
     * 修改职位
     *
     * @param updateDto
     * @return
     */
    @Override
    public Boolean update(JobUpdateDto updateDto) throws Exception {

        // 根据ID查询
        Job jobDB = jobDao.getById(updateDto.getId());
        if (jobDB == null) {
            throw JobException.jobNotExist();
        }

        Job job = JSON.parseObject(JSON.toJSONString(updateDto), Job.class);
        return jobDao.update(job);
    }

    @Override
    public Boolean add(JobAddDto addDto) throws Exception {

        // 在当前公司，当前部门下职位编码不能重复
        Job jobDB = jobDao.getByCompanyIdAndDeptId(addDto.getCompanyId(), addDto.getDepartmentId(), addDto.getJobCode());
        if (jobDB != null) {
            throw JobException.repeatJobCode();
        }
        // 在当前公司，当前部门下职位名称不能重复
        jobDB = jobDao.getByCompanyIdAndDeptIdAndJobName(addDto.getCompanyId(), addDto.getDepartmentId(), addDto.getJobName());
        if (jobDB != null) {
            throw JobException.repeatJobName();
        }

        var job = JSON.parseObject(JSON.toJSONString(addDto), Job.class);
        job.setId(snowflakeIdGenerator.nextId());

        return jobDao.add(job);
    }
}




